/**
 * @FileName	: ToDoDBAdapter.java
 * @Project		: TodoList
 * @Date		: 2010. 5. 23. 
 * @Author		: chitacan
 * @Description	:
 **/
package com.company.todolist;

import java.sql.SQLException;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

/**
 * @Date		: 2010. 5. 23. 
 * @Author		: chitacan
 * @Description	: adapter class serving access to "todoList.db"
 **/
public class ToDoDBAdapter {
	private static final String DATABASE_NAME = "todoList.db";
	private static final String DATABASE_TABLE = "todoItems";
	private static final int DATABASE_VERSION = 1;
	
	private SQLiteDatabase db;
	private final Context context;
	
	public ToDoDBAdapter(Context _context){
		this.context = _context;
		dbHelper = new todoDBOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	public static final String KEY_ID = "_id";
	public static final String KEY_TASK = "task";
	
	public static final int TASK_COLUMN = 1;
	public static final String KEY_CREATION_DATE = "creation_date";
	public static final int CREATION_DATE_COLUMN = 2;
	
	private todoDBOpenHelper dbHelper;
	
	public void close(){
		db.close();
	}
	
	public void open() throws SQLiteException {
		try {
			db = dbHelper.getWritableDatabase();
		} catch (SQLiteException e) {
			db = dbHelper.getReadableDatabase();
		}
	}
	
	public long inserTask(ToDoItem _task){
		ContentValues newTaskValues = new ContentValues();
		
		newTaskValues.put(KEY_TASK, _task.getTask());
		newTaskValues.put(KEY_CREATION_DATE, _task.getCreated().getTime());
		
		return db.insert(DATABASE_TABLE, null, newTaskValues);
	}
	
	public boolean removeTask(long _rowIndex){
		return db.delete(DATABASE_TABLE, KEY_ID + "=" + _rowIndex, null) > 0;
	}
	
	public boolean updateTask(long _rowIndex, String _task){
		ContentValues newValue = new ContentValues();
		
		newValue.put(KEY_TASK, _task);
		
		return db.update(DATABASE_TABLE, newValue, KEY_ID + "=" + _rowIndex, null) > 0; 
	}
	
	public Cursor getAllToDoItemsCursor(){
		return db.query(DATABASE_TABLE, new String[]{KEY_ID, KEY_TASK, KEY_CREATION_DATE}, null, null, null, null, null);
	}
	
	public Cursor setCursorToDoItem(long _rowIndex) throws SQLException{
		Cursor result = db.query(true, DATABASE_TABLE, new String[]{KEY_ID, KEY_TASK}, KEY_ID + "=" + _rowIndex, null, null, null, null, null);
		if((result.getCount() == 0) || !result.moveToFirst()){
			throw new SQLException("No todo item found for row: " + _rowIndex);
		}
		
		return result; 
	}
	
	public ToDoItem getToDoItem(long _rowIndex) throws SQLException{
		Cursor cursor = db.query(true, DATABASE_TABLE, new String[]{KEY_ID, KEY_TASK}, KEY_ID + "=" + _rowIndex, null, null, null, null, null);
		
		if((cursor.getCount() == 0) || !cursor.moveToFirst()){
			throw new SQLException("No todo item found for row: " + _rowIndex);			
		}
		
		String task = cursor.getString(TASK_COLUMN);
		long created = cursor.getLong(CREATION_DATE_COLUMN);
		
		ToDoItem result = new ToDoItem(task, new Date(created));
		
		return result;
	}
			
	private static class todoDBOpenHelper extends SQLiteOpenHelper{
		
		private static final String DATABASE_CREATE = "create table " + DATABASE_TABLE + " (" + KEY_ID + 
		" integer primary key autoincrement, " + KEY_TASK + " text not null, " + KEY_CREATION_DATE + " long); ";

		public todoDBOpenHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		/*
		 * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
		 * @Date		: 2010. 5. 24. 
		 * @Author		: chitacan
		 * @Description	: create data base
		 * @param db
		 */
		@Override
		public void onCreate(SQLiteDatabase _db) {
			_db.execSQL(DATABASE_CREATE);
		}

		/*
		 * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
		 * @Date		: 2010. 5. 24. 
		 * @Author		: chitacan
		 * @Description	: if data base that user want to access is already existed, destroy old one
		 * @param db
		 * @param oldVersion
		 * @param newVersion
		 */
		@Override
		public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
			Log.w("TaskDBAdapter", "Upgrading from version " + oldVersion + " to" + newVersion + ", which will destroy all ol data");
			_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(_db);
		}
		
	}
	
}
